package com.ms.action.basic;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eddy.util.AjaxOut;
import com.eddy.util.EntityReflect;
import com.eddy.util.ExtLimit;
import com.ms.dao.entity.basic.ResourcesEty;
import com.ms.dao.mapper.basic.ResourcesDao;

@Controller
@RequestMapping("/HTML/basic/ResourcesAction")
public class ResourcesAction {

	private static final long serialVersionUID = 6155643073305033053L;
	
	@Autowired
	private ResourcesDao resourcesDao;
	
	@RequestMapping("/search.action")
	public void search(HttpServletRequest request, HttpServletResponse response) {
		try {
			ResourcesEty resourcesEty = (ResourcesEty) EntityReflect.createObjectFromRequest(request, ResourcesEty.class);
			ExtLimit limit = (ExtLimit) EntityReflect.createObjectFromRequest(request, ExtLimit.class);
			resourcesEty.setExtLimit(limit);
			int count = resourcesDao.selectLimitCount(resourcesEty);
			List list = resourcesDao.selectByLimit(resourcesEty);
			AjaxOut.responseJSonGrid(response, list, count, new SimpleDateFormat("yyyy-MM-dd"));
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/searchJSONP.action")
	public void searchJSONP(HttpServletRequest request, HttpServletResponse response) {
		try {
			ResourcesEty resourcesEty = (ResourcesEty) EntityReflect.createObjectFromRequest(request, ResourcesEty.class);
			ExtLimit limit = (ExtLimit) EntityReflect.createObjectFromRequest(request, ExtLimit.class);
			resourcesEty.setExtLimit(limit);
			int count = resourcesDao.selectLimitCount(resourcesEty);
			List list = resourcesDao.selectByLimit(resourcesEty);
			AjaxOut.responseJSonPGrid(response, list, count, new SimpleDateFormat("yyyy-MM-dd"));
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/save.action")
	public void save(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		try {
			ResourcesEty resourcesEty = (ResourcesEty) EntityReflect.createObjectFromRequest(request, ResourcesEty.class);
			if(resourcesEty.getId() == null) {
				resourcesDao.insert(resourcesEty);
			} else { 
				resourcesDao.updateById(resourcesEty);
			}
			obj.put("result","success");		}
		catch (Exception e) {
			e.printStackTrace();
		}
		AjaxOut.responseText(response, obj.toString());
	}

	@RequestMapping("/delete.action")
	public void delete(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		try {
			String id = request.getParameter("id");
			resourcesDao.deleteById(Integer.parseInt(id));
			obj.put("result","success");
		}
		catch (Exception e) {
			e.printStackTrace();
			obj.put("result","error");
			obj.put("info",e.getMessage());
		}
		AjaxOut.responseText(response, obj.toString());
	}

	@RequestMapping("/getDetailInfo.action")
	public void getDetailInfo(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		try {
			String id = request.getParameter("id");
			ResourcesEty resourcesEty = (ResourcesEty) resourcesDao.selectById(Integer.parseInt(id));
			obj.put("data", resourcesEty);
		}
		catch (Exception e) {
			e.printStackTrace();
			obj.put("result","error");
			obj.put("info",e.getMessage());
		}
		AjaxOut.responseText(response, obj.toString());
	}
}